<template>
  <div>
    <el-card class="box-card" shadow="never" border="false">
    <div class="header">
      <el-form :model="queryObj" ref="queryObj" label-width="100px">
         <el-form-item label="关键字" label-width="60px">
      <el-input  class="search" v-model="queryObj.keyword" placeholder="根据编号搜索"></el-input>
       </el-form-item>
      </el-form>
      <div>
        <el-button @click="clear">清空</el-button>
      <el-button type="primary" @click="searchName">搜索</el-button>
      </div>

    </div>
      <!-- 搜索部分 end -->
      <!-- 消息记录 -->
      <el-alert
        :title="`共 ${total} 条记录`"
        effect="dark"
        :closable="false"
        show-icon
        type="info"
      >
      </el-alert>
      <!-- 表格部分 -->
      <el-table
        :data="tableData"
        v-loading="loading"
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column align="center" label="序号" prop="id">
        </el-table-column>
        <el-table-column label="题型" prop="questionType">
           <template  #default="{ row }">
           <span v-if="row.questionType ==='1'">单选</span>
           <span v-else-if="row.questionType ==='2'">多选</span>
           <span v-else-if="row.questionType ==='3'">简答</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="题目编号" prop="company" >
           <template  #default="{ row }">
           <span  style="color:#0099ff">{{row.questionIDs[0].number}}</span>

          </template>
        </el-table-column>
       <el-table-column align="center" label="录入时间" prop="addTime">
        </el-table-column>
        <el-table-column align="center" label="答题时间(s)" prop="totalSeconds">
        </el-table-column>
        <el-table-column align="center" label="正确率(%)" prop="accuracyRate">
        </el-table-column>
        <el-table-column align="center" label="录入人" prop="userName">
        </el-table-column>

        <el-table-column
          align="center"
          label="操作"
          width="280"
          class-name="small-padding fixed-width"
        >
          <template #default="{ row }">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="del(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <PageTool
          :total="total"
          :paginationPage="queryObj.page"
          :paginationPagesize="queryObj.pagesize"
          @pageChange="onPageChange"
          @pageSizeChange="onPageSizeChange"
        ></PageTool>
      </div>
    </el-card>

  </div>
</template>

<script>
import PageTool from '@/module-manage/components/page-tool'
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
export default {
  components: {
    PageTool
  },
  data () {
    return {
      type: '',
      loading: true,
      tableData: [],
      total: 0,
      queryObj: {
        page: 1,
        pagesize: 10,
        keyword: ''
      }
    }
  },

  async created () {
    this.initData()
  },

  methods: {
    async initData () {
      const { data } = await randoms(this.queryObj)
      console.log(data)
      this.total = data.counts
      this.tableData = data.items
      this.loading = false
    },
    onPageSizeChange (val) {
      this.queryObj.pagesize = val
      this.queryObj.page = 1
      this.initData()
    },
    onPageChange (val) {
      this.queryObj.page = val
      this.initData()
    },

    // 搜索
    searchName () {
      this.initData()
    },
    // 清除
    clear () {
      this.queryObj.keyword = ''
      this.initData()
    },
    // 打开弹窗

    async del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const obj = { id }
        await removeRandoms(obj)
        this.initData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  computed: {
    text () {
      if (this.type === 'add') return '创建'
      if (this.type === 'edit') return '编辑'
      else return 'add'
    }
  }
}
</script>

<style scoped lang="less">
.header{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
/deep/ .el-button--medium.is-circle {
  margin-right: 5px;
}
/deep/ .el-alert--info.is-dark {
  background-color: #f4f4f5;
  color: #909399;
}
/deep/ .el-table th {
  background-color: #fafafa;
}
.box-card {
  margin: 10px auto;
  width: 95%;
  padding: 20px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  -webkit-transition: 0.3s;
  transition: 0.3s;
  .pagination {
    margin-top: 10px;
    float: right;
  }
}
.header {
  margin-bottom: 10px;
  .search {
    width: 200px;
    margin-right: 10px;
  }
}
</style>
